<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>定时任务管理</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script src="../../assets/js/jquery-2.0.3.min.js"></script>
    <script src="../../assets/js/bootstrap.min.js"></script>
    <script src="../../assets/js/bootstrap.table/bootstrap-table.min.js" type="text/javascript"></script>
    <script src="../../assets/js/bootstrap.table/bootstrap-table-zh-CN.min.js" type="text/javascript"></script>
    <script src="../../assets/js/jquery.ztree.all.min.js"></script>
    <script src="../../assets/js/dialog.js"></script>
    <script src="../../assets/js/jquery.cookie.js" type="text/javascript"></script>
    <script src="../../assets/js/common.js" type="text/javascript"></script>
    <script src="../../assets/js/myUrl.js" type="text/javascript"></script>

    <link href="../../assets/css/bootstrap.min.css" rel="stylesheet" />
    <link href="../../assets/css/bootstrap.table/bootstrap-table.min.css"/>
    <link href="../../assets/css/dialog.css" rel="stylesheet" />
    <link href="../../assets/css/zTreeStyle.css" rel="stylesheet" />
</head>
<style type="text/css">
    a,a:hover,a:visited,a:link{
        cursor: pointer;
        text-decoration: none;
    }
</style>
<script type="text/javascript">

$(function () {
    initTable();
    $("#btn_add").on("click",function () {
        addQuartz();
    });

    $("#btn_edit").on("click", function () {
        editQuartz();
    });

    $("#btn_refuse").on("click",function () {
        initTable();
    });

    $("#btn_delete").on("click",function () {
        deleteQuartz();
    });

});

function initTable() {
    commonInitTable(local_url.quartz_list, "mytab", null);
}

function addQuartz(){
    clearInput();
    var elem1 = document.getElementById('addQuartzForm');
    dialog({
        title: "新增定时任务",
        content: elem1,
        width: '40em',
        okValue: "确定",
        ok: function () {
            if(saveCheckDataInfo()){
                var pid = $("#parentId").val();
                var dataValue = {};
                dataValue["jobName"] = $("#jobName").val();
                dataValue["jobGroup"] = $("#jobGroup").val();
                dataValue["jobClassName"] = $("#jobClassName").val();
                dataValue["cronExpression"] = $("#cronExpression").val();
                dataValue["invokeParam"] = $("#invokeParam").val();
                dataValue["desciption"] = $("#desciption").val();
                var b = saveCommon(local_url.quartz_add, dataValue, "PUT");
                if(b){
                    initTable();
                }
                return b;
            }else {
                return false;
            }
        },
        cancelValue: '取消',
        cancel: function () {}
    }).showModal();
}

function editQuartz(){
    clearInput();
    var valueInfo = $('#mytab').bootstrapTable('getSelections');
    if(valueInfo!=null&&valueInfo!=""&&valueInfo.length>0) {
        putValue(valueInfo[0]);
        var elem1 = document.getElementById('addQuartzForm');
        dialog({
            title: "修改定时任务",
            content: elem1,
            width: '40em',
            okValue: "确定",
            ok: function () {
                if (saveCheckDataInfo()) {
                    var pid = $("#parentId").val();
                    var dataValue = {};
                    dataValue["jobName"] = $("#jobName").val();
                    dataValue["jobGroup"] = $("#jobGroup").val();
                    dataValue["jobClassName"] = $("#jobClassName").val();
                    dataValue["cronExpression"] = $("#cronExpression").val();
                    dataValue["invokeParam"] = $("#invokeParam").val();
                    dataValue["desciption"] = $("#desciption").val();
                    dataValue["triggerName"] = valueInfo[0].triggerName;
                    dataValue["triggerGroup"] = valueInfo[0].triggerGroup;
                    var b = saveCommon(local_url.quartz_edit, dataValue, "PUT");
                    if (b) {
                        initTable();
                    }
                    return b;
                } else {
                    return false;
                }
            },
            cancelValue: '取消',
            cancel: function () {
            }
        }).showModal();
    }else {
        promptDail('请选择一条数据!');
    }
}

function deleteQuartz() {
    var valueInfo = $('#mytab').bootstrapTable('getSelections');
    if(valueInfo!=null&&valueInfo!=""&&valueInfo.length>0){
        var options = {"jobName": valueInfo[0].jobName, "jobGroup": valueInfo[0].jobGroup};
        removeCommon(local_url.quartz_delete, options);
    }else {
        promptDail("请选择一条数据！");
    }
}

function suspend(jobName, jobGroup) {
    var options = {"jobName": jobName, "jobGroup": jobGroup};
    dialog({
        title: "暂停",
        content: "确认暂停任务",
        width: '100px',
        okValue: "确定暂停",
        ok : function () {
            var result =  myAjax(options, local_url.quartz_suspend, "PUT");
            if(result.code=="200"){
                promptDail("暂停成功！");
            }else if(result.code!="5000") {
                promptDail("暂停失败！");
            }

        },
        onremove: function () {
            initTable();
        },
        cancelValue: '取消',
        cancel: function () {}
    }).showModal();
}

function resume(jobName, jobGroup) {
    var options = {"jobName": jobName, "jobGroup": jobGroup};
    dialog({
        title: "恢复",
        content: "确认恢复任务",
        width: '100px',
        okValue: "确定暂停",
        ok : function () {
            var result =  myAjax(options, local_url.quartz_resume, "PUT");
            if(result.code=="200"){
                promptDail("恢复成功！");
            }else if(result.code!="5000") {
                promptDail("恢复失败！");
            }

        },
        onremove: function () {
            initTable();
        },
        cancelValue: '取消',
        cancel: function () {}
    }).showModal();
}


function clearInput(){
    $("#jobName").val("");
    $("#jobGroup").val("");
    $("#jobClassName").val("");
    $("#cronExpression").val("");
    $("#invokeParam").val("");
    $("#desciption").val("");
}

function putValue(selectInfo) {
    $("#jobName").val(selectInfo.jobName);
    $("#jobGroup").val(selectInfo.jobGroup);
    $("#jobClassName").val(selectInfo.jobClassName);
    $("#cronExpression").val(selectInfo.cronExpression);
    $("#desciption").val(selectInfo.description);
}

function saveCheckDataInfo() {
    var b1 = checkValue("jobName");
    var b2 = checkValue("jobGroup");
    var b3 = checkValue("jobClassName");
    var b4 = checkValue("cronExpression");
    if(b1&&b2&&b3&&b4){
        return true;
    }

    return false;
}
</script>
<body style="padding:5px;">
<div id="toolbar" class="btn-group pull-right"  style="margin: 0px 0px 5px 0px;">
    <button id="btn_refuse" type="button" class="btn btn-default btn-purview" >
        <span class="glyphicon glyphicon-refresh"  ></span>刷新
    </button>
    <button id="btn_add" type="button" class="btn btn-default btn-purview">
        <span class="glyphicon glyphicon-plus" ></span>新增
    </button>
    <button id="btn_edit" type="button" class="btn btn-default btn-purview" >
        <span class="glyphicon glyphicon-pencil"  ></span>修改
    </button>
    <button id="btn_delete" type="button" class="btn btn-default btn-purview" >
        <span class="glyphicon glyphicon-remove" ></span>删除
    </button>
</div>
<table id="mytab" data-toggle="table" class="table table-hover table-bordered" data-single-select="true"
       data-toolbar-align="right" data-toolbar="#toolbar" data-pagination="true"  data-page-size="10"
       data-page-list="[10,20,30]" data-pagination-h-align="right" data-pagination-detail-h-align="left"
       data-pagination-loop="false" data-click-to-select="true" >
    <colgroup>
        <col style="width: 2%">
        <col style="width: 10%">
        <col style="width: 10%">
        <col style="width: 10%">
        <col style="width: 13%">
        <col style="width: 10%">
        <col style="width: 20%">
    </colgroup>
    <thead>
    <tr >
        <th data-align="center" data-valign="middle" data-checkbox="true" ></th>
        <th data-align="center" data-valign="middle" data-field="jobName">工作名称</th>
        <th data-align="center" data-valign="middle" data-field="jobGroup">工作组</th>
        <th data-align="center" data-valign="middle" data-field="jobClassName">工作类</th>
        <th data-align="center" data-valign="middle" data-field="cronExpression">任务执行方式</th>
        <th data-align="center" data-valign="middle" data-field="triggerState" data-visible="false">状态</th>
        <th data-align="center" data-valign="middle" data-field="triggerState" data-formatter="initFormat">状态</th>
        <th data-align="center" data-valign="middle" data-field="description">描述</th>
        <th data-align="center" data-valign="middle" data-field="description"data-formatter="initEdit" >操作</th>
        <th data-align="center" data-valign="middle" data-field="triggerName" data-visible="false">tigger(修改任务需要)</th>
        <th data-align="center" data-valign="middle" data-field="triggerGroup" data-visible="false">tigger(修改任务需要)</th>
    </tr>
    </thead>
</table>

<div id="addQuartzForm" style="display: none;">
    <div style="width: 100%;margin-top: 10px;height: 260px;overflow-x: hidden;overflow-y: auto;">
        <form class="form-horizontal" id="nodeDataForm" role="form">

            <div class="form-group">
                <label class="col-sm-3 control-label">任务名称:<span style="color: red;">*</span></label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" style="width: 80%" id="jobName" name="jobName" onBlur="onBlurValue(this)"
                           placeholder="任务名称"/>
                    <span class="help-block" style="color: red; display: none;">任务名称</span>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">任务分组:<span style="color: red;">*</span></label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" style="width: 80%" id="jobGroup" name="jobGroup" onBlur="onBlurValue(this)"
                           placeholder="任务分组"/>
                    <span class="help-block" style="color: red; display: none;">任务分组</span>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">执行任务类名:<span style="color: red;">*</span></label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" style="width: 80%" id="jobClassName" name="jobClassName"
                           onBlur="onBlurValue(this)" placeholder="执行任务类名"/>
                    <span class="help-block" style="color: red; display: none;">执行任务类名</span>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">时间表达式:<span style="color: red;">*</span></label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" style="width: 80%" id="cronExpression" name="cronExpression"
                           onBlur="onBlurValue(this)"  placeholder="例：(5 * * * * ?)"/>
                    <span class="help-block" style="color: red; display: none;">时间表达式</span>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">需要传递的参数:</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" style="width: 80%" id="invokeParam" name="invokeParam"
                           placeholder="json格式"/>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">工作描述:</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" style="width: 80%" id="desciption" name="desciption"
                           placeholder="工作描述"/>
                </div>
            </div>
        </form>
    </div>
</div>

</body>
<script type="text/javascript">
    function initFormat(val, row, index) {
        if(val=="WAITING"){
            return "等待";
        }else if(val=="PAUSED"){
            return "暂停";
        }else if(val=="ACQUIRED"){
            return "正常执行";
        }else if(val=="BLOCKED"){
            return "阻塞";
        }else if(val=="ERROR"){
            return "错误";
        }
    }

    function initEdit(val, row, index) {
        var html = "";
        html += "<a href=\'javascript:void(0)\' onclick=suspend(\'"+row.jobName+"\',\'"+row.jobGroup+"\')>【暂停任务】</a>"
        html += "<a href='javascript:void(0)' onclick=resume(\'"+row.jobName+"\',\'"+row.jobGroup+"\')>【恢复任务】</a>"
        return html;
    }


</script>
</html>